/* eslint-disable indent,space-before-function-paren */
import axios from 'axios'

const bas = process.env.NODE_ENV === 'development' ? 'http://localhost:8000' : 'http://118.89.227.78:81'
// let bas = 'http://localhost:8000'
let base = `${bas}/admin`

function createInstance() {
    let instance = axios.create({baseURL: base})
    instance.defaults.headers.common['X-XSRF-TOKEN'] = `${sessionStorage.getItem('token')}`
    return instance
}

function responseCatchError(msg) {
    switch (msg.response.status) {
        case 401:
            sessionStorage.removeItem('token')
            setTimeout(function () {
                window.location.href = `http://${window.location.host}/#/logIn`
            }, 0)
            console.log(`${window.location.host}/#/logIn`)
    }
}

function responseFilter(response) {
    // console.log(response)
    let {status, data} = response
    if (status >= 200 && status < 300) {
        return data
    }

    switch (status) {
        case 401: {
            localStorage.removeItem('token')
            // pageJump("/login");
            return
        }
        default: {
            return Promise.reject(data.message)
        }
    }
}

let loginInstance = axios.create({
    baseURL: base
})

// 登陆接口
export const LogIn = params => {
    return loginInstance.post(`/logIn`, params).then(responseFilter).catch(responseCatchError)
}
export const LogOut = params => {
    return loginInstance.post(`/logOut`, params).then(responseFilter).catch(responseCatchError)
}

export const addAdmin = params => {
    return createInstance().post(`/add`, params).then(responseFilter).catch(responseCatchError)
}
export const updateAdmin = params => {
    return createInstance().post(`/update`, params).then(responseFilter).catch(responseCatchError)
}
export const deleteAdmin = params => {
    return createInstance().post(`/delete`, params).then(responseFilter).catch(responseCatchError)
}
export const getAdmin = params => {
    return createInstance().get(``, {params: params}).then(responseFilter).catch(responseCatchError)
}

// 用户管理
export const getUserList = params => {
    return createInstance().get(`/user/list`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const deleteUser = params => {
    return createInstance().post(`/user/delete`, params).then(responseFilter).catch(responseCatchError)
}
// 群管理
export const getGroupList = params => {
    return createInstance().get(`/group/list`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const deleteGroup = params => {
    return createInstance().post(`/group/delete`, params).then(responseFilter).catch(responseCatchError)
}
// 动态
export const getPostList = params => {
    return createInstance().get(`/post/list`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const deletePost = params => {
    return createInstance().post(`/post/delete`, params).then(responseFilter).catch(responseCatchError)
}
// 评论
export const getCommentList = params => {
    return createInstance().get(`/comment/list`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const deleteComment = params => {
    return createInstance().post(`/comment/delete`, params).then(responseFilter).catch(responseCatchError)
}
// 文章
export const getArticleList = params => {
    return createInstance().get(`/article`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const deleteArticle = params => {
    return createInstance().post(`/article/delete`, params).then(responseFilter).catch(responseCatchError)
}
export const AddArticle = params => {
    return createInstance().post(`/article`, params).then(responseFilter).catch(responseCatchError)
}
export const UpdateArticle = params => {
    return createInstance().post(`${base}/article/update`, params).then(responseFilter).catch(responseCatchError)
}

// 通知
export const getNoticeList = params => {
    return createInstance().get(`/notice/list`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const deleteNotice = params => {
    return createInstance().post(`/notice/delete`, params).then(responseFilter).catch(responseCatchError)
}

export const AddNotice = params => {
    return createInstance().post(`/notice/add`, params).then(responseFilter).catch(responseCatchError)
}
export const UpdateNotice = params => {
    return createInstance().post(`/notice/update`, params).then(responseFilter).catch(responseCatchError)
}

/* 轮播图 */
export const getCarouselList = params => {
    return createInstance().get(`/carouse/list`, {params: params}).then(responseFilter).catch(responseCatchError)
}
export const deleteCarouse = params => {
    return createInstance().post(`/carouse/delete`, params).then(responseFilter).catch(responseCatchError)
}

export const AddCarouse = params => {
    return createInstance().post(`/carouse/add`, params).then(responseFilter).catch(responseCatchError)
}
export const UpdateCarouse = params => {
    return createInstance().post(`/carouse/update`, params).then(responseFilter).catch(responseCatchError)
}

export const getImageUrl = x => {
    return `${bas}${x}`
}

export const uploadPic = params => {
    return createInstance().post(`/file/upload`, params).then(responseFilter).catch(responseCatchError)
}
